#!/usr/bin/perl -w

use Data::Dumper;

use perlmod::camera;

#create the camera object
my $camera = perlmod::camera->new();

#load a shotlist
my $shotlist = $camera->getshotlist();

#set target to sd card
$shotlist->enqueue( [ "set-config-index capturetarget=1\n" , "capture-image\n" ] );

foreach $n (reverse(1..52)) {
	
	$shotlist->enqueue( [ "set-config-index shutterspeed=$n\n" , "capture-image\n" ] );
	$shotlist->enqueue( [ "capture-image\n" ] );
	$shotlist->enqueue( [ "capture-image\n" ] );
	
}

foreach (1..156) {
	
	$shotlist->enqueue( [ "capture-image\n" ] );
	
}

foreach $n (1..52) {
	
	$shotlist->enqueue( [ "set-config-index shutterspeed=$n\n" , "capture-image\n" ] );
	$shotlist->enqueue( [ "capture-image\n" ] );
	$shotlist->enqueue( [ "capture-image\n" ] );
	
}

$shotlist->enqueue( [ "quit\n" ] );


#connect to the hardware
print "main thread calling connect\n";
$camera->connect();

#play out the shotlist
$camera->beginshotlist();

#hang
while (1) {
	
	if ( $camera->{'ipc'}->{'outputqueue'}->pending() ) {
		
		print $camera->{'ipc'}->{'outputqueue'}->dequeue();
		
	}
	
}


#done!
$camera->disconnect();


print "all done\n";
exit();



my $capturecount = 0;


#hang on queue
while (1) {
	
	while ( $camera->{'ipc'}->{'outputqueue'}->pending() ) {
		
		$response = $camera->{'ipc'}->{'outputqueue'}->dequeue_nb();
		
		if ( $response =~ /Saving file as .*\..*/ )
		{
			
			$capturecount += 1;
			
			if ( $capturecount < 22 ) {
				print "exposure index: " . ( 52 - $capturecount ) . "\n";
				$camera->writetocamera("set-config-index shutterspeed=" . ( 52 - $capturecount ) . "\n");
			}
			
			print "Sending Capture Command $capturecount\n";
			$camera->writetocamera("capture-image-and-download\n");
			
			print "Sleeping\n";
			
		}
		
	}
	
	#if the capture is complete
	if ($capturecount > 300) {
		
		#make this the last run
		last;
		
	}
}








print "main thread calling disconnect\n";
$camera->disconnect();

print "main thread done\n";




